#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QFontDialog>
#include<QFileDialog>
#include<QFile>
#include<QMessageBox>
#include"convertdialog.h"
#include<QTextCodec>



TextStatus::TextStatus()
    :newFileContent(false)
    ,openFileChanged(false)
    ,hasFileContent(false)
    ,opened(false)
    ,newed(false)
    ,saved(true)
    ,coding("UTF-8")
{

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,te(new QTextEdit(this))
    ,nameLabel(new QLabel(this))
    ,codeLabel(new QLabel(this))
{
    ui->setupUi(this);
    this->setMinimumSize(640,480);
    this->setWindowTitle("Notepad");
    this->setWindowIcon(QIcon(":/icon/edit_3.png"));


    this->setCentralWidget(te);
    ui->statusbar->addWidget(nameLabel);
    ui->statusbar->addWidget(codeLabel);

    connect(te,&QTextEdit::textChanged,[this](){
        this->setWindowTitle("Notepad *");
        ts.saved = false;
    });

    te->setLineWrapMode(QTextEdit::NoWrap);
    //set default font
    te->setFont(QFont("Arial",18));
//    nameLabel->setText("filename");

    codeLabel->setText("UTF-8");


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::reopenWithCoding(const char *coding){

    if(ts.currentFilePath.isEmpty()){
        return;
    }

    QFile f(ts.currentFilePath);
    if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&f);
            in.setCodec(coding);
            te->setText(in.readAll());
            ts.saved=true;
            this->setWindowTitle("Notepad");
            codeLabel->setText(coding);
            ts.coding = coding;
     }
    f.close();
    nameLabel->setText(ts.currentFilename);

    qDebug() << "open file with coding:" << coding;
}


void MainWindow::on_actionnew_triggered()
{
    QString s = te->toPlainText();
//    qDebug() << s;
    if(!s.isEmpty()){
        //save
        this->on_actionsave_triggered();

    }
    if(ts.saved){
        te->clear();
        nameLabel->clear();
        ts.currentFilename.clear();
        ts.currentFilePath.clear();
        this->setWindowTitle("Notepad");
        ts.saved = false;
    }

    qDebug() << "new file over";
}

void MainWindow::on_actionfont_triggered()
{
//    auto fd = new QFontDialog(this);
//    te->setFontPointSize(18);
//    qDebug()<<te->font();
//    fd->setCurrentFont(te->font());
//    fd->show();
//    qDebug()<<fd->selectedFont();

    auto f = QFontDialog::getFont(nullptr,te->font());
//    qDebug() << f;
//    auto s = te->toPlainText();
//    te->clear();
//    te->setText(s);
    te->setFont(f);
    qDebug()<< "font settings over";

}

void MainWindow::on_actionsave_triggered()
{
//    qDebug() << te->toHtml();
//    qDebug() << te->toMarkdown();
//    qDebug() << te->toPlainText();

    if(ts.currentFilename.isEmpty()){
        ts.currentFilePath = QFileDialog::getSaveFileName();
        if(ts.currentFilePath.isEmpty()){
            return;
        }
//        qDebug()<<ts.currentFilePath;
        auto p = ts.currentFilePath;
        ts.currentFilename = p.mid(p.lastIndexOf("/")+1);
        nameLabel->setText(ts.currentFilename);
//        qDebug()<<ts.currentFilename;
//        qDebug() << ts.currentFilePath;
        QFile f(ts.currentFilePath);
        if(f.open(QIODevice::WriteOnly|QIODevice::Text)){
            QTextStream out(&f);
            out.setCodec(ts.coding.toLatin1().data());
            out<<te->toPlainText();
            qDebug() << "save file:"<<ts.currentFilename;
            ts.saved = true;
        }
        f.close();
    }else{
        QFile f(ts.currentFilePath);
        if(f.open(QIODevice::WriteOnly|QIODevice::Text)){
            QTextStream out(&f);
            out.setCodec(ts.coding.toLatin1().data());
            out<<te->toPlainText();
            qDebug() << "save file:"<<ts.currentFilename;
            ts.saved = true;
        }
        nameLabel->setText(ts.currentFilename);
        f.close();
    }
    if(ts.saved){
        this->setWindowTitle("Notepad");
    }

}

void MainWindow::on_actionopen_triggered()
{
    ts.currentFilePath = QFileDialog::getOpenFileName();
    auto p = ts.currentFilePath;
    ts.currentFilename = p.mid(p.lastIndexOf("/")+1);
    qDebug()<<ts.currentFilename;
    qDebug()<<ts.currentFilePath;

    QFile f(ts.currentFilePath);
    if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&f);
            in.setCodec(ts.coding.toLatin1().data());
            te->setText(in.readAll());
            ts.saved=true;
            this->setWindowTitle("Notepad");
     }
    nameLabel->setText(ts.currentFilename);
    f.close();
    qDebug() << "open file over";
}

void MainWindow::on_actionclose_triggered()
{

    if(!te->toPlainText().isEmpty()){
        if(ts.currentFilename.isEmpty()){
            int r = QMessageBox::warning(this,"警告","文件有内容是否保存？",QMessageBox::Ok,QMessageBox::Cancel);
            if(r == QMessageBox::Ok){
                ts.currentFilePath = QFileDialog::getSaveFileName();
                if(!ts.currentFilePath.isEmpty()){
                    auto p = ts.currentFilePath;
                    ts.currentFilename = p.mid(p.lastIndexOf("/")+1);
                    QFile f(ts.currentFilePath);
                    if(f.open(QIODevice::WriteOnly|QIODevice::Text)){
                        QTextStream out(&f);
        //                out.setCodec("UTF-8");
                        out.setCodec(ts.coding.toLatin1().data());
                        out<<te->toPlainText();
                        qDebug() << "save file:"<<ts.currentFilename;
                        QMessageBox::information(this,"保存","文件已保存");
                    }
                    nameLabel->setText(ts.currentFilename);

                    f.close();
                }


            }
        }else{
            if(!ts.saved){
                auto r = QMessageBox::warning(this,
                                              "关闭文件",
                                              "是否保存？"
                                              ,QMessageBox::Ok|QMessageBox::Cancel);

                if(r == QMessageBox::Ok){
                    QFile f(ts.currentFilePath);
                    if(f.open(QIODevice::WriteOnly|QIODevice::Text)){
                        QTextStream out(&f);
        //                out.setCodec("UTF-8");
                        out.setCodec(ts.coding.toLatin1().data());
                        out<<te->toPlainText();
                        qDebug() << "save file:"<<ts.currentFilename;
                        QMessageBox::information(this,"保存","文件已保存");
                    }
                    nameLabel->setText(ts.currentFilename);
                    f.close();
                }
            }
        }
    }


    ts.currentFilename.clear();
    ts.currentFilePath.clear();
    nameLabel->clear();
    ts.coding = "UTF-8";
    codeLabel->setText(ts.coding);
    te->clear();
    this->setWindowTitle("Notepad");

    ts.saved = true;



    qDebug() << "close file";
}

void MainWindow::on_actionconvert_triggered()
{

//    auto d = new ConvertDialog(this);
//    qDebug() << "dialog pointer address:" << d;
//    d->show();

    ConvertDialog d(ts.currentFilePath,ts.coding);
    d.exec();//block is ok
//    d.show();//no block,flashing 闪现


    qDebug() << "convert dialog opened";
}

void MainWindow::on_actionGB2312_triggered()
{
    reopenWithCoding("GB2312");

}

void MainWindow::on_actionUTF_8_triggered()
{
    reopenWithCoding("UTF-8");
}

void MainWindow::on_actionGBK_triggered()
{
    reopenWithCoding("GBK");
}

void MainWindow::on_actionASCII_triggered()
{
    reopenWithCoding("ASCII");
}

void MainWindow::on_actionUTF_16_triggered()
{
    reopenWithCoding("UTF-16");
}

void MainWindow::on_actionUTF_32_triggered()
{
    reopenWithCoding("UTF-32");
}

void MainWindow::on_actionLatin_1_triggered()
{
    reopenWithCoding("Latin1");
}
